<?php


namespace app\admin\controller;


use app\common\model\V_station;
use app\common\model\Station;
use app\common\validate\Vld_station;

class Stations extends Base
{
    public  function  get_detail()
    {//获取详细信息
        $id=input('post.id');
        $data = V_station::field('id ,ip,name,response_mem,response_cpu,response_pid1,up_time,mem_total,mem_used,mem_usedp,cpu,pid1_name,pid1_state,pid1_mem ')
            ->where('id',$id)
            ->find();
        //返回判断
        if ($data){
            $msg='OK';
            $status=200;
        }else{
            $msg='failed';
            $status=0;
        }
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>$data,
            'meta'=>$meta
        ];
        return json($res);
    }
    public  function  get_stations_list()
    {//工作站列表方法 

        $list = Station::field(' id ,name ')           
            ->order(['id'=>'asc'])           
            ->select();
        //返回判断
        if ($list){
            $msg='OK';
            $status=200;
        }else{
            $msg='failed';
            $status=0;
        }
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>$list,
            'meta'=>$meta
        ];
        return json($res);
    }

    public  function  get_list()
    {//栏目列表方法

       
        $pagenum=input('post.pagenum'); //// 当前页码数
        $pagesize=input('post.pagesize'); //// 当前每页有多少条          

        $list = V_station::field(' id ,isalert,ip,name,response_cpu,response_mem,response_cpu,response_pid1,response_cpid1,up_time ')           
            ->order(['id'=>'asc'])
            ->paginate([
                'list_rows'=> $pagesize,
                'page' => $pagenum,
            ])
            ->toArray();
        //返回判断
        if ($list){
            $msg='OK';
            $status=200;
        }else{
            $msg='failed';
            $status=0;
        }
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>$list,
            'meta'=>$meta
        ];
        return json($res);
    }
    public function get_sid()
    {//获取客户端SID
        $ip= request()->ip();
        $mydata = Station::field('id')
        ->where('ip', $ip)
        ->find();
        $res=[
            'ip'=>$ip,            
            'data'=>$mydata
        ];
        return "<div id='data'>".$mydata['id']."</div>";
        return json($res);

    }
    public function edit_alert()
    {//修改报警
        $data=[            
            'id'=>input('post.id'),
            'isalert'=>input('post.isalert')           
        ];
        try
        {
            $validateres = validate(Vld_station::class)
                ->scene('edit')
                ->check($data);
            if( $validateres)
            {//通过验证
                $mydata = Station::where('id',$data['id'])
                    ->find();
                $result= $mydata->save($data);
                if($result){
                    //判断修改是否成功
                    $msg="ok";
                    $status=200;
                }else{
                    $msg="fail";
                    $status=0;
                }
            }
        }
        catch (ValidateException  $e) {
            // 验证失败 输出错误信息
            $msg=  $e->getMessage();
            $status=0;
        }
        // 返回客户端信息
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>null,
            'meta'=>$meta
        ];
        return json($res);

    }
}
